﻿using System;
using System.Collections.Generic;
using System.Data;
using Dao;
using Domain.Common;

namespace Domain.Officers
{
    /// <summary>
    /// 文武将
    /// </summary>
    public partial class Officer
    {
        private Random rd = new Random(System.DateTime.Now.Millisecond);

        public void UpdateVisiteOfficer()
        {
            if(this.WorkType!=Domain.Officers.WorkType.VisiteOfficer)
                return;
            //回城
            if (HistoryDate.CurrentDate >= this.VisiteOfficer.BackTime)
            {
                this.InBuilding = this.BelongBuilding;
                bool ret = GetVisiteOfficerResult();
                //登庸成功，设置武将归属
                if (ret)
                {
                   Officer targetOfficer= this.VisiteOfficer.TargetOfficer;
                   targetOfficer.BelongBuilding.OwnedOfficers.Remove(targetOfficer);
                   targetOfficer.BelongBuilding.InBuildingOfficers.Remove(targetOfficer);
                   targetOfficer.BelongBuilding = this.BelongBuilding;
                    this.BelongBuilding.OwnedOfficers.Add(targetOfficer);
                    this.BelongBuilding.InBuildingOfficers.Add(targetOfficer);
                    this.UpdateVisiteOfficerformance(targetOfficer);

                }
                this.WorkType = null;
            }
        }
        /// <summary>
        /// 登庸是否成功。随机数算法
        /// </summary>
        /// <returns></returns>
        private bool GetVisiteOfficerResult()
        {

            //double ret0 = this.Reputation * 0.5;
            double ret1 = this.Glamour * 0.5;
            double ret2 = this.Intelligence * 0.3;
            double ret3 = this.Politics * 0.2;
            double total = ret1 + ret2 + ret3;
            double percent = total / 300;
            double rdret = rd.NextDouble();
            if (percent >= rdret)
                return true;
            else
                return false;
        }
        #region  内政功绩
        /// <summary>
        /// 更新武将发展农业的功绩
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public int UpdateDevelopCulturePerformance(int days)
        {
            int ret = (int)((this.Politics + this.Intelligence) / 2 * 0.1 * days);
            this.Performance += ret;
            return ret;
        }
        /// <summary>
        /// 更新武将发展技术的功绩
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public int UpdateDevelopTechnologyPerformance(int days)
        {
            int ret = (int) (this.Intelligence*0.1*days);
            this.Performance += ret;
            return ret;
        }
        /// <summary>
        /// 更新武将发展商业的功绩
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public int UpdateDevelopCommercePerformance(int days)
        {
            int ret = (int)(this.Politics * 0.1 * days);
            this.Performance += ret;
            return ret;
        }
        /// <summary>
        /// 更新武将发展农业的功绩
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public int UpdateDevelopAgriculturePerformance(int days)
        {
            int ret = (int)(this.Politics * 0.1 * days);
            this.Performance += ret;
            return ret;
        }
        /// <summary>
        /// 更新武将治安的功绩
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public int UpdateDevelopPublicSecurityPerformance(int days)
        {
            int ret = (int)((this.Strength+this.Commander)/2 * 0.1 * days);
            this.Performance += ret;
            return ret;
        }
        /// <summary>
        /// 更新武将开展民心的功绩
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public int UpdateDevelopPopularFeelingsPerformance(int days)
        {
            int ret = (int)((this.Glamour + this.Reputation/10) / 2 * 0.1 * days);
            this.Performance += ret;
            return ret;
        }
        /// <summary>
        /// 更新武将城池加固的功绩
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public int UpdateDevelopEndurancePerformance(int days)
        {
            int ret = (int)((this.Politics + this.Commander) / 2 * 0.1 * days);
            this.Performance += ret;
            return ret;
        }
        /// <summary>
        /// 更新武将征兵的功绩
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public int UpdateDevelopSoldierPerformance(int days)
        {
            int ret = (int)((this.Politics + this.Commander+this.Glamour+this.Reputation/10) / 4 * 0.1 * days);
            this.Performance += ret;
            return ret;
        }
        /// <summary>
        /// 更新武将扩建城墙的功绩
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public int UpdateDevelopExtendBuildingPerformance(int days)
        {
            int ret = (int)((this.Politics + this.Commander) / 2 * 0.1 * days);
            this.Performance += ret;
            return ret;
        }
        /// <summary>
        /// 更新武将登庸的功绩
        /// </summary>
        /// <param name="targetOfficer"></param>
        /// <returns></returns>
        public int UpdateVisiteOfficerformance(Officer targetOfficer)
        {
            int ret = (targetOfficer.Politics + targetOfficer.Commander + targetOfficer.Glamour +
                       targetOfficer.Intelligence + targetOfficer.Strength + targetOfficer.Reputation/10);
            this.Performance += ret;
            return ret;
        }
        #endregion
        /// <summary>
        /// 赠送宝物商品给他人
        /// </summary>
        /// <param name="treasure"></param>
        /// <param name="targetOfficer"></param>
        public void GiveToTargetOfficer(Treasure treasure, Officer targetOfficer)
        {
            this.HasTreasures.Remove(treasure);
            targetOfficer.HasTreasures.Add(treasure);
            targetOfficer.LoyaltyIndex += treasure.LoyaltyAdd;
            if (targetOfficer.LoyaltyIndex > 100)
                targetOfficer.LoyaltyIndex = 100;

        }
    }
}
